package com.gxa.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.pojo.Level1;
import com.gxa.pojo.Level2;
import com.gxa.service.Level1Service;
import com.gxa.utils.DataCheck;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: saas
 * @description: 商品一级分类
 * @author: Tirion
 * @create: 2020-09-24 23:00
 **/

@Controller
@ControllerAdvice
@RequestMapping("/level1")
@CrossOrigin
@Api(value = "商品一级分类",tags = "商品分类一级api")
public class Level1Controller {

    @Resource
    private Level1Service level1Service;

    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "一级分类列表",
            notes = "获取一级分类商品信息", httpMethod = "post", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "startIndex", value = "当前页",
             dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",
                    dataType = "int", paramType = "query")
    })
    public Map<String, Object> list(@RequestParam(value = "startIndex",required = false,defaultValue = "0") Integer startIndex,
                                    @RequestParam(value = "pageSize",required = false,defaultValue = "0") Integer pageSize) {
        PageHelper.startPage(startIndex, pageSize);
        List<Map<Object, Object>> maps = level1Service.selectCount();
        JsonResult jsonResult = new JsonResult();
        DataCheck.check(jsonResult, maps);
        PageInfo<Map<Object, Object>> level1PageInfo = new PageInfo<>(maps);
        long total = level1PageInfo.getTotal();
        jsonResult.put("total", total);
        return jsonResult.getValues();
    }

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除一级商品接口",
            notes = "设置商品status=1", httpMethod = "get", response = String.class)
    public Map<String, Object> delete(@RequestParam @ApiParam(name = "id",
            value = "目标品牌id", required = true) Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (id == null) {
            jsonResult.setMsg("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        int i = level1Service.deleteByPrimaryKey(id);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }
    @PostMapping("/deleteBatch")
    @ResponseBody
    @ApiOperation(value = "批量删除",
            notes = "设置商品status=1", httpMethod = "post", response = String.class)
    public Map<String, Object> delete(@RequestParam @ApiParam(name = "list",
            value = "目标品牌id集合", required = true) List<Integer> list) {
        JsonResult jsonResult = new JsonResult();
        if (list == null) {
            jsonResult.setMsg("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        int i = level1Service.deleteBatch(list);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }

    @PostMapping("/save")
    @ResponseBody
    @ApiOperation(value = "添加接口", notes = "将json中对应的数据注入pojo对象",
            httpMethod = "post", response = String.class)
    public Map<String, Object> save(@RequestBody @ApiParam(name = "level1",
            value = "一级商品pojo信息", required = true) Level1 level1) {
        JsonResult jsonResult = new JsonResult();
        if (level1 == null) {
            jsonResult.setCode("403");
            jsonResult.setMsg("参数为空");
            return jsonResult.getValues();
        }
        Level1 list = level1Service.selectByName(level1.getName());
        if (list != null) {
            jsonResult.setMsg("品牌已存在");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        int i = level1Service.insertSelective(level1);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改接口", notes = "将json中对应的数据注入pojo对象",
            httpMethod = "post", response = String.class)
    public Map<String, Object> update(@RequestBody @ApiParam(name = "level1",
            value = "一级商品pojo信息", required = true) Level1 level1) {
        JsonResult jsonResult = new JsonResult();
        if (level1 == null) {
            jsonResult.setCode("403");
            jsonResult.setMsg("参数为空");
            return jsonResult.getValues();
        }
        int i = level1Service.updateByPrimaryKeySelective(level1);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }

    @GetMapping("/selectById")
    @ResponseBody
    @ApiOperation(value = "根据id查询",
            notes = "回显修改信息", httpMethod = "get", response = String.class)
    public Map<String, Object> selectById(@RequestParam @ApiParam(name = "id",
            value = "目标对象id", required = true) Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (id == null) {
            jsonResult.setMsg("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Level1 level1 = level1Service.selectByPrimaryKey(id);
        DataCheck.check(jsonResult, level1);
        return jsonResult.getValues();
    }

    @PostMapping("/selectJunior")
    @ResponseBody
    @ApiOperation(value = "查看下级接口",
            notes = "查看当前品牌的下一级属性", httpMethod = "post", response = String.class)
    public Map<String, Object> selectJunior(@RequestBody @ApiParam(name = "id",
            value = "当前品牌id", required = true) Map<String,Integer> map) {
        JsonResult jsonResult = new JsonResult();
        Integer id = map.get("id");
        Integer integer1 =  map.get("startIndex");
        Integer integer2 =  map.get("pageSize");
        if (id == null) {
            jsonResult.setMsg("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Page<Object> objects = PageHelper.startPage(integer1, integer2);
        List<Level2> level2 = level1Service.selectJunior(id);
        DataCheck.check(jsonResult, level2);
        PageInfo<Object> pageInfo = new PageInfo<>(objects);
        jsonResult.put("total",pageInfo.getTotal());
        return jsonResult.getValues();
    }



}
